﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CandleStickChart.Stock;

namespace FXSStation.CustomControls.CandleStickChart
{
    public class CandleStickChart : Control
    {
        private ChartStyle cs = new ChartStyle();
        public DataCollection dc = new DataCollection();
        public DataSeries ds = new DataSeries();
        private Canvas chartCanvas;
        private Canvas textCanvas;
        private Grid chartGrid;
        private TextBox txYmin;
        private TextBox txYmax;
        private TextBox txYTick;
        private TextBlock tbTitle;
        private TextBlock tbXLabel;
        private TextBlock tbYLabel;

        static CandleStickChart()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CandleStickChart), new FrameworkPropertyMetadata(typeof(CandleStickChart)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            chartCanvas = base.Template.FindName("chartCanvas", this) as Canvas;
            textCanvas = base.Template.FindName("textCanvas", this) as Canvas;
            chartGrid = base.Template.FindName("chartGrid", this) as Grid;
            chartGrid = base.Template.FindName("chartGrid", this) as Grid;

            txYmin = base.Template.FindName("txYmin", this) as TextBox;
            txYmax = base.Template.FindName("txYmax", this) as TextBox;
            txYTick = base.Template.FindName("txYTick", this) as TextBox;

            tbTitle = base.Template.FindName("tbTitle", this) as TextBlock;
            tbXLabel = base.Template.FindName("tbXLabel", this) as TextBlock;
            tbYLabel = base.Template.FindName("tbYLabel", this) as TextBlock;


            chartGrid.SizeChanged += new SizeChangedEventHandler(chartGrid_SizeChanged);
            AddChart();

        }

        private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            textCanvas.Width = chartGrid.ActualWidth;
            textCanvas.Height = chartGrid.ActualHeight;
            chartCanvas.Children.Clear();
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);

            AddChart();
        }

        public void AddChart()
        {
            if (dc.DataList.Count > 0)
            {
                cs = new ChartStyle();
                cs.ChartCanvas = chartCanvas;
                cs.TextCanvas = textCanvas;
                cs.XMin = -1;
                cs.XMax = ds.DataItems.Count;
                cs.XTick = 3;
                cs.YMin = double.Parse(txYmin.Text);
                cs.YMax = double.Parse(txYmax.Text);
                cs.YTick = double.Parse(txYTick.Text);
                cs.Title = "Stock Chart";
                cs.AddChartStyle(tbTitle, tbXLabel, tbYLabel, ds);
                dc.StockChartType = DataCollection.StockChartTypeEnum.Candle;
                dc.AddStockChart(cs);
            }
        }
        public void InsertItem(StockItem item)
        {
            textCanvas.Width = chartGrid.ActualWidth;
            textCanvas.Height = chartGrid.ActualHeight;
            chartCanvas.Children.Clear();
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
            if (dc.DataList.Count > 0)
            {
                cs = new ChartStyle();
                cs.ChartCanvas = chartCanvas;
                cs.TextCanvas = textCanvas;
                cs.XMin = -1;
                cs.XMax = ds.DataItems.Count;
                cs.XTick = 3;
                cs.YMin = double.Parse(txYmin.Text);
                cs.YMax = double.Parse(txYmax.Text);
                cs.YTick = double.Parse(txYTick.Text);
                cs.Title = "Stock Chart";
                cs.AddChartStyle(tbTitle, tbXLabel, tbYLabel, ds);
                dc.StockChartType = DataCollection.StockChartTypeEnum.Candle;
                dc.AddNewPoin(cs, ds, item);

            }
        }

    }
}
